#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fstream>
using std::ifstream;

// This is the only file that needs to be changed in order to support 
// big endian systems' file IO

unsigned int readInt(FILE *f)
{
	unsigned int i;
	fread(&i, 1, sizeof(int), f);
	return i;
}

float readFloat(FILE *f)
{
	float d;
	fread(&d, 1, sizeof(float), f);
	return d;
}

float readFloat(ifstream &fInp)
{
	float d;
	fInp.read( (char*) &d, sizeof(float) );
	return d;
}

char * readAsciizString(FILE *f)
{
	char temp[256];
	int i;
	i = 0;
	while ((temp[i] = fgetc(f))) i++;
	temp[i] = 0;

	char * res = new char[i+1];
	memcpy(res,temp,i+1);

	return res;
}

char * readAsciizString(ifstream &fInp)
{
	char temp[256];
	int i;
	i = 0;
	bool foundNullTerminator = false;
	while ( fInp.good() && !foundNullTerminator )
	{
		char c;
		fInp.read( (char*) &c, sizeof(char) );
		
		if( c == '\0' ) foundNullTerminator = true;
		else{
			temp[i] = c;
			i++;
		}
		
	}
	temp[i] = 0;

	char * res = new char[i+1];
	memcpy(res,temp,i+1);

	return res;
}

char readByte( ifstream &fInp )
{
	char d;
	fInp.read( (char*) &d, sizeof(char) );
	return d;
}

unsigned int readUint( ifstream &fInp )
{
	unsigned int d;
	fInp.read( (char*) &d, sizeof(unsigned int) );
	return d;
}

